//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package impl

import (
	"net/url"
	"sync"

	"github.com/dchest/uniuri"

	"database/sql"
)

type ClientInfo struct {
	Name          string
	Driver        string
	DataSource    string
	rawDataSource string
	client        *sql.DB
}

func (c *ClientInfo) NewPgClientWithDb(db string) (*sql.DB, error) {
	u, err := url.Parse(c.rawDataSource)
	if err != nil {
		return nil, err
	}
	u.Path = "/" + db
	return sql.Open(c.Driver, u.String())
}

var clientMap = sync.Map{}
var token string

func Init() string {
	token = uniuri.NewLenChars(64, []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"))
	return token
}
